lib/fetcher: Factor out HTTP status code handling from soup and curl
authorPhilip Withnall <withnall@endlessm.com>
Wed, 30 May 2018 11:20:49 +0000 (12:20 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Wed, 30 May 2018 16:23:57 +0000 (16:23 +0000)
Use the same G_IO_ERROR_* values for HTTP status codes in both fetchers.
The libsoup fetcher still handles a few more internal error codes than
the libcurl one; this could be built on in future.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Closes: #1594
Approved by: jlebon

src/libostree/ostree-fetcher-curl.c
src/libostree/ostree-fetcher-soup.c
src/libostree/ostree-fetcher-util.c
src/libostree/ostree-fetcher-util.h

index c0f38131012f9c04d2c8b4804c833b66f7ca671c..2e090cfa07975beca0d6306471108378cb93c03e 100644 (file)
@@ -337,19 +337,7 @@ check_multi_info (OstreeFetcher *fetcher)
           curl_easy_getinfo (easy, CURLINFO_RESPONSE_CODE, &response);
           if (!is_file && !(response >= 200 && response < 300))
             {
-              GIOErrorEnum giocode;
-
-              /* TODO - share with soup */
-              switch (response)
-                {
-                case 404:
-                case 403:
-                case 410:
-                  giocode = G_IO_ERROR_NOT_FOUND;
-                  break;
-                default:
-                  giocode = G_IO_ERROR_FAILED;
-                }
+              GIOErrorEnum giocode = _ostree_fetcher_http_status_code_to_io_error (response);
 
               if (req->idx + 1 == req->mirrorlist->len)
                 {
index 43794275d4d17d6f394861cd1e0a3d3c06d0b905..3951a927f37abee5bdf1bdf6aa209f422a509133 100644 (file)
@@ -1070,19 +1070,13 @@ on_request_sent (GObject        *object,
                 soup_uri_to_string (soup_request_get_uri (pending->request), FALSE);
 
               GIOErrorEnum code;
+
               switch (msg->status_code)
                 {
-                case SOUP_STATUS_NOT_FOUND:
-                case SOUP_STATUS_FORBIDDEN:
-                case SOUP_STATUS_GONE:
-                  code = G_IO_ERROR_NOT_FOUND;
-                  break;
+                /* These statuses are internal to libsoup, and not standard HTTP ones: */
                 case SOUP_STATUS_CANCELLED:
                   code = G_IO_ERROR_CANCELLED;
                   break;
-                case SOUP_STATUS_REQUEST_TIMEOUT:
-                  code = G_IO_ERROR_TIMED_OUT;
-                  break;
                 case SOUP_STATUS_CANT_RESOLVE:
                 case SOUP_STATUS_CANT_CONNECT:
                   code = G_IO_ERROR_HOST_NOT_FOUND;
@@ -1095,7 +1089,8 @@ on_request_sent (GObject        *object,
 #endif
                   break;
                 default:
-                  code = G_IO_ERROR_FAILED;
+                  code = _ostree_fetcher_http_status_code_to_io_error (msg->status_code);
+                  break;
                 }
 
               {
index 9cdb82c6c167c87ca0518d6e5ebb39eb31a357b0..6f759c86e86cb6e1efad07faff574667af31c6a9 100644 (file)
@@ -218,3 +218,22 @@ _ostree_fetcher_should_retry_request (const GError *error,
 
   return FALSE;
 }
+
+/* Convert a HTTP status code representing an error from libsoup or libcurl to
+ * a #GIOError. This will return %G_IO_ERROR_FAILED if the status code is
+ * unknown or otherwise unhandled. */
+GIOError
+_ostree_fetcher_http_status_code_to_io_error (guint status_code)
+{
+  switch (status_code)
+    {
+    case 403:  /* SOUP_STATUS_FORBIDDEN */
+    case 404:  /* SOUP_STATUS_NOT_FOUND */
+    case 410:  /* SOUP_STATUS_GONE */
+      return G_IO_ERROR_NOT_FOUND;
+    case 408:  /* SOUP_STATUS_REQUEST_TIMEOUT */
+      return G_IO_ERROR_TIMED_OUT;
+    default:
+      return G_IO_ERROR_FAILED;
+    }
+}
index 5f62ad45b77c463da9b91d534c48963b4eabf285..1cade0686025bee7a6bd4eecfec3c8d6ec224eac 100644 (file)
@@ -78,6 +78,8 @@ void _ostree_fetcher_journal_failure (const char *remote_name,
 gboolean _ostree_fetcher_should_retry_request (const GError *error,
                                                guint         n_retries_remaining);
 
+GIOError _ostree_fetcher_http_status_code_to_io_error (guint status_code);
+
 G_END_DECLS
 
 #endif